package xin.javashare.javacollectionsframework;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

/**
 * 程序清单22.7 P29【数组线性表和链表】
 */
public class TestArrayAndLinkedList {
    public static void main(String[] args) {
        List<Integer> arrayList = new ArrayList<Integer>();
        arrayList.add(1);
        arrayList.add(2);
        arrayList.add(3);
        arrayList.add(1);
        arrayList.add(4);
        arrayList.add(0, 10); // 在指定下标0处添加一个新元素
        arrayList.add(3, 30);

        System.out.println("A list of Inegers in the array list:");
        System.out.println(arrayList);

        LinkedList<Object> linkedList = new LinkedList<>(arrayList);
        linkedList.add(1, "red");
        linkedList.removeLast();
        linkedList.addFirst("gree");

        System.out.println("Dispay the linked list forward:");
//		System.out.println(linkedList); // 显示的是一个数组
        ListIterator<Object> listIerator = linkedList.listIterator();
        while (listIerator.hasNext()) {
            System.out.print(listIerator.next() + " ");
        }
        System.out.println();

        System.out.println("Dispay the linked list backward:");
        // 返回从linkedList.size()开始的元素所用的迭代器
        listIerator = linkedList.listIterator(linkedList.size());
        while (listIerator.hasPrevious()) {
            System.out.print(listIerator.previous() + " ");
        }
//		System.out.println("\n" + listIerator.next());
    }
}
